<script setup>
import { onMounted, reactive, ref } from "vue";
import Table from "../../../components/Table/table";
import Pagination from "@/components/Pagination/Pagination";
import { getMailboxListApi, delMailboxApi } from "../../../api/mailbox";
import { mailboxTable } from "@/hooks/mailbox";
import { useCommunityNameList, useMailboxStatus } from "@/hooks/index";
import { ElMessage } from "element-plus";
import addForm from "./addForm.vue";
import { formatTimes } from "../../../utils/Fw-data";

// ===================================================================
const addRef = ref();
const list = ref([]);
const total = ref(0);
const getCommunityNameList = useCommunityNameList();
const getMailboxStatus = useMailboxStatus();
// ---------------------------------
// 处理状态文字
const getStatusText = (val) => {
  let res = getMailboxStatus.find((item) => item.value === val * 1);
  // console.log(res);
  return res ? res.label : "无效";
};
// ---------------------------------
const queryData = reactive({
  pageSize: 10,
  current: 1,
  community_code: "",
  content: "", //信件内容
  reply: "", //回复内容
  community: "", //小区名称
  member: "", //发件人
  title: "", //信箱标题
  status: null, //状态
  create_time: "",
});

onMounted(() => {
  initData();
});
// ====================表格功能
const initData = async () => {
  const {
    data: { count, rows },
  } = await getMailboxListApi(queryData);
  console.log(rows);
  list.value = rows;
  total.value = count;
};

// 处理时间
const timeArr = ref([]);
watch(timeArr, (val) => {
  console.log(timeArr.value);
  if (timeArr.value) {
    queryData.create_time = val
      .map((item) => {
        item = +new Date(item);
        return item;
      })
      .join(",");
    console.log(queryData.create_time);
  } else {
    queryData.create_time = "";
  }
});

// 查询
const onSearch = async () => {
  queryData.current = 1;
  initData();
};
// 重置
const onReset = async () => {
  Object.assign(queryData, {
    pageSize: 10,
    current: 1,
    community_code: "",
    content: "", //信件内容
    reply: "", //回复内容
    community: "", //小区名称
    member: "", //发件人
    title: "", //信箱标题
    status: null, //状态
    create_time: "",
  });
  initData();
};
// 修改
const edit = (id) => {
  addRef.value.replyData(id);
};
// 删除
const del = async (id) => {
  await delMailboxApi(id);
  ElMessage.success("删除成功");
  if (queryData.current > 1 && list.value.length === 1) queryData.current--;
  initData();
};
</script>
<template>
  <div class="page-box">
    <div class="top">
      <span class="h_tit">信箱管理</span>
      <span class="text"
        >用于管理小区里业主的信箱信息，包括工作建议，意见反馈等。(app端过来的数据)</span
      >
    </div>

    <div class="utils">
      <el-form label-width="80px" inline="true">
        <el-form-item label="小区名称" style="width: 200px">
          <el-select v-model="queryData.community_code" placeholder="请输入">
            <el-option
              v-for="item in getCommunityNameList"
              :key="item.id"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动主题" style="width: 200px">
          <el-input placeholder="请输入" />
        </el-form-item>
        <el-form-item label="状态" style="width: 200px">
          <el-select v-model="queryData.status" placeholder="请输入">
            <el-option
              v-for="item in getMailboxStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" style="width: 280px">
          <el-date-picker
            v-model="timeArr"
            @change="changeCreate"
            type="daterange"
            start-placeholder="请输入"
            end-placeholder="请输入"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="onReset">重置</el-button>
          <el-button @click="onSearch" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-row type="flex" justify="space-between">
        <div></div>
        <div class="tab_utils">
          <el-icon color="#409EFC" class="no-inherint"> </el-icon>
          <el-icon color="#409EFC" :icon="Share"> </el-icon>
          <el-icon color="#409EFC" class="no-inherint"> </el-icon>
        </div>
      </el-row>
      <br />
      <Table :table="mailboxTable" :tableData="list" @edit="edit" @del="del">
        <!-- 小区名称 -->
        <template #community.name="{ row }">
          {{ row.community?.name }}
        </template>
        <!-- 发信人 -->
        <template #member.name="{ row }">
          {{ row.member?.name }}
        </template>
        <!-- 状态 -->
        <template #status="{ row }">
          {{ getStatusText(row.status) }}
        </template>
        <template #create_time="{ row }">
          {{ formatTimes(row.create_time) }}
        </template>
      </Table>
    </div>

    <!-- 分页区域 -->
    <Pagination
      v-model:pageNum="queryData.current"
      v-model:pageSize="queryData.pageSize"
      :total="total"
      @initData="initData"
    />

    <!-- 新增表单组件 -->
    <addForm ref="addRef" @updateList="initData" />
  </div>
</template>
<style scoped>
.page-box {
  margin: 40px;
  min-height: 500px;
  background-color: #fbfbfc;
}

.h_tit {
  font-weight: 700;
  font-size: 18px;
}

.text {
  font-size: 15px;
  margin-left: 20px;
  color: #575858;
}

.top {
  margin: 10px;
}

.utils {
  margin-left: 5px;
  margin-right: 5px;
  padding: 20px;
  background-color: #fff;
  box-shadow: 5px 5px 5px 3px rgba(186, 177, 177, 0.2);
}

.table {
  margin: 5px;
  padding: 20px;
  margin-top: 10px;
  background-color: #fff;
  box-shadow: 5px 5px 5px 3px rgba(186, 177, 177, 0.2);
}
</style>
